//
//  DBTool.swift
//  微博项目
//
//  Created by admin on 16/3/14.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class DBTool: NSObject {
    static var queue:FMDatabaseQueue? =
    {
        // 拼接文件名
        let filePath = "\(NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!)database.sqlite"
//        let filePath = "/Users/admin/testSQLite3.sqlite"
        // 实例化
        return FMDatabaseQueue(path: filePath)
    }()
    class func query(sql:String,withArgumentsInArray:[AnyObject]!,success:(result:FMResultSet?)->(Void))
    {
        self.queue?.inDatabase({ (database:FMDatabase!) -> Void in
            let res:FMResultSet? = database.executeQuery(sql, withArgumentsInArray: withArgumentsInArray)
            success(result: res)
        })
    }
    class func execute(sql:String,withArgumentsInArray:[AnyObject]!,success:((bool:Bool)->(Void))?)
    {
        self.queue?.inDatabase({ (database:FMDatabase!) -> Void in
            let res = database.executeUpdate(sql, withArgumentsInArray: withArgumentsInArray)
            success?(bool: res)
        })
    }
}
